//
//  MoreViewController.m
//  斗鸡TV
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "MoreViewController.h"
#import "MoreNewsViewCell.h"
#import "ApplicitonClass.h"
#import "AVPlayerViewController.h"
#import "NewsViewCell.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define Live_Url @"http://www.waqu.com/v4/qudan/videoCards.json?appName=general_video&appVersion=3.6.6&fTitle=true&isMakeQudan=0&partner=0100&platform=ios&posByHistory=false&profile=general_men&qdid=561ba43c2c687252f31e1473&sid=cd18452f65ece0686810e3b45c97857f&size=10&start=0"
@interface MoreViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,LoadHttpDataDelegate>
{
    UICollectionView * collectionV;
    UITableView * tbView;
    NSMutableArray * allArray;
    SubWaQuModel * sHM;
}
@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    allArray = [NSMutableArray new];

    
    UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(WIDTH/2-2, 150);
    layout.minimumInteritemSpacing = 1;
    
    collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:layout];
    collectionV.backgroundColor = [UIColor clearColor];
    collectionV.delegate = self;
    collectionV.dataSource = self;
    [collectionV registerClass:[NewsViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collectionV];
    
    [self loadMore];
    
    self.title = @"更多列表";
}

-(void)loadMore
{
    
    [self showLoading];
    NSString * str = [NSString stringWithFormat:@"http://www.waqu.com/v4/qudan/videoCards.json?appName=general_video&appVersion=3.6.6&fTitle=true&isMakeQudan=0&partner=0100&platform=ios&posByHistory=false&profile=general_men&qdid=%@&sid=cd18452f65ece0686810e3b45c97857f&size=1000&start=0",self.wuquModel.id];
    
    //使用代理封装AFN
    ApplicitonClass * appLiciton = [ApplicitonClass getInstance];
    [appLiciton httpGetUrl:str params:nil];
    appLiciton.delegate = self;
}

//网络请求成功delegate
-(void)successWithResponse:(id)response
{
     [self dismissLoading];
    NSDictionary * dict = response;
    NSLog(@"%@",dict);
    if (dict) {
        
        NSArray * array = [dict arrayForKey:@"cards"];
        if (array)
        {
            for (NSDictionary * dic in array) {
                
                NSDictionary * dict = [dic dictionaryForKey:@"video"];
                
                sHM = [[SubWaQuModel alloc] initWithDictionary:dict error:nil];
                 [allArray addObject:sHM];
            }
            
        }
       
        NSLog(@"%@",allArray);
    }
    
    [collectionV reloadData];
}

//网络请求失败delegate
-(void)failedWithResponse:(id)request Error:(NSError *)error
{
     
    NSLog(@"%@",[NSString stringWithFormat:@"%@",error]);
    [self showError:[NSString stringWithFormat:@"%@",error]];
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   return allArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewsViewCell * cell = [collectionV dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
     SubWaQuModel * swm = allArray[indexPath.row];
    [cell setWqCellData:swm];
    
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    AVPlayerViewController * avPlay = [AVPlayerViewController new];
    
    SubWaQuModel * sm = allArray[indexPath.row];
    avPlay.wuquModel = sm;
    avPlay.mid = self.wuquModel.id;
    [self.navigationController pushViewController:avPlay animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
